#include "rm_udp_canbus.hpp"

UDP_CAN::UDP_CAN(/* args */)
{
}

UDP_CAN::~UDP_CAN()
{
}

int UDP_CAN::Init_CAN(int port)
{
    ADEALL_UDP_SERVER::Init(port, this);
    return 0;
}

void UDP_CAN::CallBackFuntion(std::string ip, int port, std::vector<uint8_t> data, void *)
{
    static int cnt = 0;
    if (data.size() % 13 == 0)
    {
        uint8_t n = data.size() / 13;
        for (size_t i = 0; i < n; i++)
        {
            uint8_t index_0 = i * 13;
            uint8_t dlength = data[index_0];
            uint32_t id = (uint32_t)data[index_0 + 1] << 24 | (uint32_t)data[index_0 + 2] << 16 | data[index_0 + 3] << 8 | (uint32_t)data[index_0 + 4];
            if (dlength <= 8)
            {
                if (id <= 0x7ff) // 标准帧不大于0x7ff
                {
                    if (canCallBackprt != nullptr)
                        canCallBackprt(id, dlength, (uint8_t *)&data[index_0 + 5]);
                    else
                        canCallBackfun(id, dlength, (uint8_t *)&data[index_0 + 5]);
                    cnt++;
                }

                // data_temp.id = id;
                // data_temp.data = (uint8_t *)&data[index_0 + 5];
                // data_temp.l = dlength;
                // ThreadBase(Th_CAN_msg_cb_once, this);
            }
            else
            {
                dlength = dlength & 0x7f;
                if (dlength <= 8 && id <= 0x1fffffff) // 扩展帧
                {
                    // printf(" exp  call back!%d\n", i);

                    if (canCallBackprt != nullptr)
                        canCallBackprt(id, dlength, (uint8_t *)&data[index_0 + 5]);
                    else
                        canCallBackfun(id, dlength, (uint8_t *)&data[index_0 + 5]);

                    cnt++;
                }
            }
        }
    }

    // printf("hhhai %d [", data.size());
    // for (size_t i = 0; i < data.size(); i++)
    // {
    //     printf("%x ", data[i]);
    // }
    // printf("] cnt=%d\n", cnt);
}
// 处理一帧回调函数
void *UDP_CAN::Th_CAN_msg_cb_once(void *prt)
{
    UDP_CAN *p = (UDP_CAN *)prt;
    if (p->callback != nullptr)
        p->callback(p->context, p->data_temp.id, p->data_temp.l, p->data_temp.data);
    else
        p->canCallBackfun(p->data_temp.id, p->data_temp.l, p->data_temp.data);
    return nullptr;
}
/*
 * 以太网转can 模块发送，默认发送can1
 */
void UDP_CAN::Send(uint32_t id, uint8_t *data, uint8_t dlc, const char *ip, int port)
{
    if (dlc > 8)
        return;
    uint8_t send_buff[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    if (id > 0x7ff) // 扩展帧
    {
        send_buff[0] = dlc | 0x80;
    }
    else // 标准帧
    {
        send_buff[0] = dlc;
    }

    send_buff[1] = id >> 24 & 0xff;
    send_buff[2] = id >> 16 & 0xff;
    send_buff[3] = id >> 8 & 0xff;
    send_buff[4] = id & 0xff;

    for (size_t i = 0; i < dlc; i++)
    {
        send_buff[i + 5] = data[i];
    }

    ADEALL_UDP_SERVER::MessagSend(ip, port, (char *)send_buff, 13);
    // printf("%s  %d",ip,port);

    // printf("[udpcan]==>");
    // for (size_t i = 0; i < 13; i++)
    // {
    //     printf("%.2X ",send_buff[i]);
    // }
    // printf("\n");
}
// 测试打印
void UDP_CAN::canCallBackfun(uint32_t id, uint8_t dlc, uint8_t *data)
{
    printf("id=%X[ ", id);
    for (size_t i = 0; i < dlc; i++)
    {
        printf("%x ", data[i]);
    }
    printf("]\n");
}